from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import status
from admincharge.models import ChargeAdmin
from PetNetadmin.serializers import ShopAdminLoginSearilizer
from django.db.models import Q
from utils.pagination import PetMessagePagination
from rest_framework.decorators import api_view
from admincharge.utils.adminPermission import AdminPermission
from admincharge.utils.jwt import MyJWTAuthentication

class EditPasswordApiView(APIView):
    # 修改后台管理员的密码
    def post(self, request):
        try:
            password = request.data['pwd']
            id = request.data['id']
            updatePwd = ChargeAdmin.objects.get(id=id)
            print(updatePwd)
            data = {
                'password': password
            }
            serializer = ShopAdminLoginSearilizer(instance=updatePwd, data=data, partial=True)
            if serializer.is_valid(raise_exception=True):
                print(serializer.validated_data)
                serializer.save()
            result = {
                'code': 2000,
                'msg': '上传保存成功'
            }
            return Response(result)

        except ChargeAdmin.DoesNotExist:
            return Response({"msg": '当前没有该记录可以修改'}, status=status.HTTP_404_NOT_FOUND)

class GetAllAdminMessageApiView(APIView):
    authentication_classes = [MyJWTAuthentication]
    permission_classes = [AdminPermission]
    def get(self,request):
        username = request.GET.get('username')
        nick_name = request.GET.get('nick_name')
        status = request.GET.get('status')
        # 构建查询条件
        conditions = Q()
        if username:
            conditions &= Q(username=username)
        if nick_name:
            conditions &= Q(nick_name=nick_name)
        if status:
            conditions &= Q(status=status)
        if conditions:
            queryset = ChargeAdmin.objects.filter(conditions)
        else:
            queryset = ChargeAdmin.objects.all()
        # 分页处理
        paginator = PetMessagePagination()
        result_page = paginator.paginate_queryset(queryset, request)
        serializer = ShopAdminLoginSearilizer(instance=result_page, many=True)
        result = {
            'data': serializer.data,
            'code': 2000,
            'msg': '获取成功'
        }
        return paginator.get_paginated_response(result)

@api_view(['POST'])
def changeAdminStatus(request):
    username = request.GET.get('username')
    status = request.GET.get('status')
    print(username, status)
    admin_login = ChargeAdmin.objects.filter(username=username).first()
    admin_login.status = status
    admin_login.save()
    result = {
        'code': 2000,
        'msg': '修改成功'
    }
    return Response(result)
@api_view(['POST'])
def changeAdminSuper(request):
    username = request.GET.get('username')
    is_super = request.GET.get('is_super')
    print(username, is_super)
    admin_login = ChargeAdmin.objects.filter(username=username).first()
    print(admin_login.is_super)
    admin_login.is_super = is_super
    admin_login.save()
    result = {
        'code': 2000,
        'msg': '修改成功'
    }
    return Response(result)

